//
//  PostsDetailViewModel.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/19.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PostsDetailViewModel.h"
#import "HomeChannelDetailModel.h"
#import "TAMacro.h"
#import "HomeChannelDetailContent.h"
#import "HomeChannelDetailImg.h"
#import "HomeChannelDetailTag.h"
#import "HomeChanneldetailCommentModel.h"

#import "SelfSizeTool.h"
@interface PostsDetailViewModel ()

@property (nonatomic, assign) CGFloat currentHeight;

@end


@implementation PostsDetailViewModel

- (void)setHomeChannelDetailModel:(HomeChannelDetailModel *)homeChannelDetailModel {
    _homeChannelDetailModel = homeChannelDetailModel;
   
    
    
    [self setUpHeaderImageButtonFrame];
    [self setUpNameLabelFrame];
    [self setUpVipImageVIewFrame];
    [self setUpTimeLabelFrame];
    [self setUpPhotoScrollViewFrame];
    
    if (_homeChannelDetailModel.content.img.count > 1) {
        
        [self setUpNumPhotoLabelFrame];
        
    }
    
    [self setUpDescLabelFrame];
    [self setUpTagslabelFrame];
    //
    if (_homeChannelDetailModel.content.likeNum != 0) {
        [self setUpLineViewFrame];
        [self setUpHeartImageFrame];
    }
    
    [self setUplikePersonCollectionViewFrame];
    [self setUpMessegeImageViewFrame];
    //
    [self setUpSecLineViewFrame];
    [self setUpCommentDetailTableViewFrame];
    [self setUpDetailViewHeight];
    [self setUpScrollViewContentSize];
    
}

// 头像
- (void)setUpHeaderImageButtonFrame {
    CGFloat viewX = ViewMargin;
    CGFloat viewY = ViewMargin;
    CGFloat viewW = 35;
    CGFloat viewH = 35;
    _headerImageButtonFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}
// 昵称
- (void)setUpNameLabelFrame {
    CGFloat viewX = CGRectGetMaxX(_headerImageButtonFrame) + ViewMargin;
    CGFloat viewY = ViewMargin;
    CGFloat viewW = 200;
    CGFloat viewH = 35;
    _nameLabelFrame = CGRectMake(viewX, viewY, viewW, viewH);
}
// 会员
- (void)setUpVipImageVIewFrame {
    CGFloat viewX = CGRectGetMaxX(_headerImageButtonFrame) - 5;
    CGFloat viewY = CGRectGetMaxY(_headerImageButtonFrame) - 5;
    CGFloat viewW = 12;
    CGFloat viewH = 12;
    _vipImageViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}
// 时间
- (void)setUpTimeLabelFrame {

    
}
// 图片
- (void)setUpPhotoScrollViewFrame {
    
    HomeChannelDetailImg *img = _homeChannelDetailModel.content.img.firstObject;
    CGFloat scale = img.w / (SCREEN_WIDTH - 20);
    CGFloat height = img.h / scale;
    
    CGFloat viewY = CGRectGetMaxY(_headerImageButtonFrame) + ViewMargin;
    CGFloat viewX = 0;
    CGFloat viewW = SCREEN_WIDTH;
    CGFloat viewH = ceil(height);
    _photoScrollViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    _currentHeight = CGRectGetMaxY(_photoScrollViewFrame);
    
}
// 图片个数
- (void)setUpNumPhotoLabelFrame {
    CGFloat viewW = 40;
    CGFloat viewH = 25;
    CGFloat viewX = SCREEN_WIDTH - viewW - ViewMargin;
    CGFloat viewY = CGRectGetMaxY(_photoScrollViewFrame) - viewH - ViewMargin;
    _numPhotoLabelFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}
// 详情
- (void)setUpDescLabelFrame {
    CGFloat viewH = 0;

    if ([_homeChannelDetailModel.content.desc isEqualToString:@""]) {
        
    } else {
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:_homeChannelDetailModel.content.desc width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:14]];
        viewH = height + 2 * ViewMargin;
    }
    CGFloat viewX = 2 * ViewMargin;
    CGFloat viewY = CGRectGetMaxY(_photoScrollViewFrame);
    CGFloat viewW = SCREEN_WIDTH - 4 * ViewMargin;
    _descLabelFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}
// 标签
- (void)setUpTagslabelFrame {
   
    CGFloat labelH = 0;
    if (_homeChannelDetailModel.content.tag.count != 0) {
        NSMutableArray *tagNameArray = [NSMutableArray array];
        for (NSInteger i = 0; i < _homeChannelDetailModel.content.tag.count; i++) {
            [tagNameArray addObject:_homeChannelDetailModel.content.tag[i].content];
        }
        [tagNameArray insertObject:@"#" atIndex:0];
        NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:tagString width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:13]];
        labelH = height + 2 * ViewMargin;
        self.currentHeight += labelH;
    }
    CGFloat labelX = ViewMargin * 2;
    CGFloat labelY = CGRectGetMaxY(_descLabelFrame);
    CGFloat labelW = SCREEN_WIDTH - 40;
    _tagsLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}


- (void)setUpHeartImageFrame {
    CGFloat viewX = 17;
    CGFloat viewY = CGRectGetMaxY(_tagsLabelFrame) + ViewMargin * 2 + 5;
    CGFloat ViewW = 16;
    CGFloat viewH = 16;
    _heartImageFrame = CGRectMake(viewX, viewY, ViewW, viewH);
}

- (void)setUplikePersonCollectionViewFrame {
    CGFloat viewH = 0;
    if (_homeChannelDetailModel.content.likeUsers.count == 0) {
        
    } else if (_homeChannelDetailModel.content.likeUsers.count <= 8) {
        CGFloat height = (SCREEN_WIDTH - 60) / 8 ;
        viewH = height;
        
    } else  {
        CGFloat height = (SCREEN_WIDTH - 60) / 4 + 2;
        viewH = height;
    }
    
    CGFloat viewW = SCREEN_WIDTH - 60;
    CGFloat viewX = 50;
    CGFloat viewY = CGRectGetMaxY(_tagsLabelFrame) + ViewMargin;
    _likePersonCollectionViewFrame = CGRectMake(viewX, viewY, viewW, viewH);

    
}


- (void)setUpLineViewFrame {
    
    CGFloat viewX = 0;
    CGFloat viewY = CGRectGetMaxY(_tagsLabelFrame);
    CGFloat viewW = SCREEN_WIDTH;
    CGFloat viewH = 1;
    _lineViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}

- (void)setUpSecLineViewFrame {
    CGFloat viewX = 0;
    CGFloat viewY = CGRectGetMaxY(_likePersonCollectionViewFrame) + ViewMargin + 5;
    CGFloat viewW = SCREEN_WIDTH;
    CGFloat viewH = 1;
    _secLineViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}


- (void)setUpMessegeImageViewFrame {
    CGFloat viewX = 17;
    CGFloat viewY = CGRectGetMaxY(_likePersonCollectionViewFrame) + 2 * ViewMargin + 16;
    CGFloat viewW = 16;
    CGFloat viewH = 16;
    _messegeImageViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}

- (void)setUpCommentDetailTableViewFrame {
    
    CGFloat viewH = 0;
    for (NSInteger i = 0; i < _homeChannelDetailModel.content.comment.count; i++) {
        HomeChanneldetailCommentModel *commentModel = _homeChannelDetailModel.content.comment[i];
        if (commentModel.replyName) {
            NSString *string = [NSString stringWithFormat:@"回复 %@: %@", commentModel.replyName, commentModel.content];
            CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
            viewH += height + 35;
        } else {
            NSString *string = commentModel.content;
            CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
            viewH += height + 35;
        }
    }
    
    if (_homeChannelDetailModel.content.comment.count == 0) {
        viewH = 50;
    }
    
    CGFloat viewW = SCREEN_WIDTH - 60;
    CGFloat viewX = 50;
    CGFloat viewY = CGRectGetMaxY(_likePersonCollectionViewFrame) + ViewMargin * 2;
    _commentDetailTableViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
}

- (void)setUpDetailViewHeight {
    
    _detailViewHeight = CGRectGetMaxY(_commentDetailTableViewFrame);
    
}

- (void)setUpScrollViewContentSize {
    if (_detailViewHeight < SCREEN_HEIGHT - 64 - 49) {
        CGSize contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 + 1);
        _scrollViewContentView = contentSize;
    } else {
        CGSize contentSize = CGSizeMake(SCREEN_WIDTH, _detailViewHeight + 1);
        _scrollViewContentView = contentSize;
    }
}




@end
